﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models;
using tracker.Models.DTO;
using tracker.Models.BLL;

namespace tracker.Controllers
{
    public class HandlerADUAlarmDefineController : Controller
    {
        /// <summary>
        /// GET HandlerADUAlarmDefin 
        /// </summary>
        /// <param name="strAlarmType">Alarm Type</param>
        /// <param name="strExpression">Expression</param>
        /// <param name="strHandlerType">Add/Update</param>
        /// <returns>true/false</returns>
        [CallAjaxFilter]
        public string Index(string DefineID, string AlarmType, string Expression, string HandlerType)
        {
            string strDefineID = DefineID;
            string strAlarmType = AlarmType;
            string strExpression = Expression;
            string strHandlerType = HandlerType;

            string _username = HttpContext.Session[Common.USER].ToString();
            string _language = HttpContext.Session[Common.LANGUAGE].ToString();
            _language = _language == string.Empty ? "vi" : _language;

            if (strHandlerType == "Add")
            {
                EAlarmDefine ent = new EAlarmDefine();

                ent.CreateBy = _username;
                ent.AlarmType = ListDefineAlarm.getID(strAlarmType, _language).ToString();
                ent.Expression = strExpression;
                string strResult = AlarmDefineBLL.InsertERP(new string[]{ EAlarmDefine.FIELD_ALARMTYPE,
                                                                          EAlarmDefine.FIELD_EXPRESSION,
                                                                          EAlarmDefine.FIELD_CREATEBY
                                                            },
                                                            new object[]{ ent.AlarmType,
                                                                          ent.Expression,
                                                                          ent.CreateBy
                                                            });
                if (strResult == "0")
                    return strResult;
                else
                    return "false";

            }
            else if (strHandlerType == "Update")
            {
                EAlarmDefine ent = new EAlarmDefine();
                ent.ModifyBy = _username;
                ent.AlarmType = ListDefineAlarm.getID(strAlarmType, _language).ToString();
                ent.DefineID = strDefineID;
                ent.Expression = strExpression;
                ent.CreateBy = _username;

                string strResult = AlarmDefineBLL.UpdateERP(new string[]{ EAlarmDefine.FIELD_ALARMTYPE,
                                                                          EAlarmDefine.FIELD_EXPRESSION,
                                                                          EAlarmDefine.FIELD_CREATEBY,
                                                                          EAlarmDefine.FIELD_ID,
                                                                          EAlarmDefine.FIELD_MODIFYBY
                                                            },
                                                            new object[]{ ent.AlarmType,
                                                                          ent.Expression,
                                                                          ent.CreateBy,
                                                                          ent.DefineID,
                                                                          ent.ModifyBy
                                                            });

                if (strResult == "0")
                    return "true";
                else
                    return "false";
            }
            else if (strHandlerType == "Delete")
            {
                string strResult = AlarmDefineBLL.DeleteERP(new string[] { EAlarmDefine.FIELD_CREATEBY, EAlarmDefine.FIELD_ID },
                                                            new object[] { _username, strDefineID });

                if (strResult == "0")
                    return "true";
                else
                    return "false";
            }
            else
            {
                return "false";
            }
        }
    }
}
